package rmmk.algorithms.features;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import rmmk.framework.datasources.Document;

public class FrequentNumbersGlobal {

	private static HashMap<String, HashMap<String, Integer>> map = new HashMap<>();

	public void calculate(List<Document> trainDocuments) {

		Pattern p = Pattern
				.compile("\\d+\\,\\d+[\\s]{1}[a-zA-Z]+|\\d+\\.\\d+[\\s]{1}[a-zA-Z]+|\\d+[\\s]{1}[a-zA-Z]+");

		for (Document doc : trainDocuments) {
			try {
				// Pattern intsOnly = Pattern.compile("\\d+");
				// Pattern p =
				// Pattern.compile("[0-9]+.[0-9]*|[0-9]*.[0-9]+|[0-9]+");

				// Pattern p = Pattern.compile("\\d+[\\s]{1}[a-zA-Z]+");
				Matcher makeMatch = p.matcher(doc.getTEXT());
				// makeMatch.find();
				// String inputInt = makeMatch.group();
				// System.out.println(inputInt);
				// System.out.println("Document: " + doc.toString());
				while (makeMatch.find()) {
					// System.out.print("Start index: " + matcher.start());
					// System.out.print(" End index: " + matcher.end() + " ");
					// System.out.print(makeMatch.group() + " ");
					addToMap(doc, makeMatch.group());
				}
			} catch (Exception ex) {
			//	int i = 0;
			//	++i;
			}
		}
	}

	// Pattern number = Pattern.compile("\\d+\\,\\d+|\\d+\\.\\d+|\\d+");
	Pattern word = Pattern.compile("[a-zA-Z]+");

	private void addToMap(Document doc, String group) {
		Matcher wordMatch = word.matcher(group);
		if (wordMatch.find()) {
			String word = wordMatch.group();

			HashMap<String, Integer> stats = map.get(doc.toString());

			if(stats == null)
			{
				stats = new HashMap<String, Integer>();
				map.put(doc.toString(), stats);
			}
			
			if (stats.containsKey(word)) {
				Integer i = stats.get(word);
				stats.put(word, ++i);
			} else
				stats.put(word, 0);
			
			
		}
		else 
		{
		//	int i = 0;
		//	++i;
		}
	}

	public static HashMap<String, HashMap<String, Integer>> getMap() {
		return map;
	}

	public static void setMap(
			HashMap<String, HashMap<String, Integer>> map) {
		FrequentNumbersGlobal.map = map;
	}

	/*
	 * private void updateMap(String forClass) { if(map.containsKey(forClass)) {
	 * Integer i = map.get(forClass); map.put(forClass, ++i); } else
	 * map.put(forClass, 0); }
	 */
}
